package com.yiya.ms.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yiya.bean.Material;
import com.yiya.bean.SysUser;
import com.yiya.model.MaterialCplex;
import com.yiya.model.MaterialCplexModel;
import com.yiya.service.MaterialService;
import com.yiya.utils.HtmlUtil;
import com.yiya.utils.SessionUtils;

@Controller
@RequestMapping("/material")
public class MaterialAction extends BaseAction {
	@SuppressWarnings("unused")
	private final static Logger log = Logger.getLogger(MaterialAction.class);

	@Autowired
	private MaterialService materialService;
	
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, false));
	}
	
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request) {
		Map<String, Object> context = getRootMap();
		return forword("material/material", context);
	}
	
	@RequestMapping("/dataList")
	public void datalist(MaterialCplexModel model, HttpServletResponse response)
			throws Exception {
		List<MaterialCplex> list = materialService.queryByList(model);
		int count = materialService.queryByCount(model);
		
		Float costSum = 0f;
		List<Map<String, Object>> footer = new ArrayList<Map<String, Object>>();
		for(MaterialCplex meterial:list){
			costSum+=meterial.getCost();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "合计：");		
		map.put("cost", costSum);
		footer.add(map);
		
		// 设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("rows", list);
		jsonMap.put("total", count);
		jsonMap.put("footer", footer);
		HtmlUtil.writerJson(response, jsonMap);
	}
	
	@RequestMapping("/save")
	public void save(Material bean,HttpServletRequest request,HttpServletResponse response){
		Date nowTime = new Date();
		SysUser user = SessionUtils.getUser(request);
		if(bean.getId() == null){
			bean.setCreateTime(nowTime);
			bean.setCreateBy(user.getId());
			materialService.add(bean);
		}else{
			bean.setUpdateBy(user.getId());
			bean.setUpdateTime(nowTime);
			materialService.Update(bean);
		}
		sendSuccessMessage(response, "保存成功");
	}
	
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response){
		Map<String,Object>  context = getRootMap();
		Material material = materialService.queryById(id);
		if(material  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", material);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/delete")
	public void delete(Integer[] id,HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUser user = SessionUtils.getUser(request);
		materialService.delete(id,user);
		sendSuccessMessage(response, "删除成功");
	}

}
